#!/usr/bin/python2
# -*- coding: utf-8 -*-
# Name:     functions.py
# Author:   xiooli <xioooli[at]yahoo.com.cn>
# Version:  111108

import re
from PySide.QtCore import QCoreApplication

#regx_symbol_begin = re.compile(r'< *symbol [^<]*< *path', re.S)
#regx_id = re.compile(r'(id=[^>]*[\'\"])')
#repl_id = lambda x: '<path ' + regx_id.findall(x.group())[0]
#regx_symbol_end = re.compile(r'< */ *symbol *>')
reg_bak = re.compile(r'<rect.*(?<=rect)[^>]*fill-opacity.*>')

def tr(msg):
    return QCoreApplication.translate("@default", msg)

def fixsvg(svg):
    if not 'symbol' in svg:
        return svg
    #svg = regx_symbol_begin.sub(repl_id, svg)
    #svg = regx_symbol_end.sub('', svg)
    svg = reg_bak.sub('', svg)
    return svg.replace('symbol', 'g').replace('viewBox="0 0 600 600"', 'viewBox="0 0 620 600"')

class dict2obj():
    def __init__(self, dic):
        self.__dict__['d'] = dic

    def __getattr__(self, key):
        if self.__dict__['d'].has_key(key):
            value = self.__dict__['d'][key]
            return value
        return

def myproduct(args):
    # args is a 2d list
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

if __name__ == '__main__':
    for i in myproduct(([1,2,3], [4,5], [6,7,8])):
        print i
